#include "Application.h"
#include <GL/freeglut.h>

#include <iostream>

Application::Application():
m_gameMgr(NULL),
m_gfxMgr(NULL),
m_worldMgr(NULL)
{
	m_gameMgr = new GameManager;
	m_gfxMgr = new GraphicsManager;
	m_worldMgr = new WorldManager();
}

Application::~Application()
{
	if (NULL != m_gameMgr)
	{
		delete m_gameMgr;
	}

	if (NULL != m_gfxMgr)
	{
		delete m_gfxMgr;
	}

	if (NULL != m_worldMgr)
	{
		delete m_worldMgr;
	}
}

GameManager* Application::getGameMgr()
{
	return m_gameMgr;
}

GraphicsManager* Application::getGraphicsMgr()
{
	return m_gfxMgr;
}

WorldManager* Application::getWorldMgr()
{
	return m_worldMgr;
}

/*
*	main app loop
*/
void Application::main(int argc, char **argv)
{
	// initialize the managers!
	m_gameMgr->Initialize(argc, argv);
	m_gfxMgr->Initialize(argc, argv);
	m_worldMgr->Initialize(argc, argv);
	m_gameMgr->StartNewGame(m_worldMgr);

	glutMainLoop();
	return;
}

void Application::Tick()
{
	m_gameMgr->Tick();
	m_worldMgr->Tick();
	m_gfxMgr->Tick();
}

void Application::ProcessRendering(void)
{
	m_worldMgr->Render();
}

void Application::ProcessMouse(int x, int y) 
{
	m_gameMgr->ProcessMouse(x, y);
}

void Application::ProcessMouseClick(int button, int state, int x, int y)
{
	m_gameMgr->ProcessMouseClick(button, state, x, y);
}

void Application::KeyboardInput(unsigned char c, int x, int y)
{
	m_gameMgr->KeyboardInput(c, x, y);
}

void Application::ProcessSpecialInput(int key, int x, int y)
{
	m_gameMgr->ProcessSpecialInput(key, x, y);
}

void Application::ProcessIdle(void)
{
	Tick();
}

void Application::ProcessClose()
{
	// do nothing for now
}

void Application::Exit()
{
	exit(0);
}